#!/bin/bash
#Rachel Install Script Created by Jeremy Lawing 4-11-12

if [ "$(id -u)" != "0" ]; 
then
   echo -e "This script must be run as root.\nPlease run with sudo or login as root to run this script." 1>&2
   exit 1
fi


echo -e "Welcome to the Rachel Installer/Uninstaller/Updater!\n"
echo "Would you like to install, uninstall, or update? [install/uninstall/update]: "
read response

if [ "$response" = "install" ] || [ "$response" = "uninstall" ] || [ "$response" = "update" ]
then
	echo -e "\nAre you sure you want to $response? [yes/no]: "
	read yesno

	if [ "$yesno" == "no" ]
	then
		echo "Thanks for using the Rachel Installer."
		exit 0
	fi
		
	if [ "$response" == "install" ]
	then
		

		echo -e "\n---------------Installing ssh---------------1/8"
		apt-get -y install ssh		
		echo -e "\n---------------Installing expect---------------2/8"
		apt-get -y install expect	
		echo -e "\n---------------Installing git---------------3/8"
		apt-get -y install git	
		echo -e "\n---------------Installing gtk---------------4/8"
		apt-get -y install libgtk-3-dev
		echo -e "\n---------------Installing gobject---------------5/8"
		apt-get -y install python-gobject-dev
		echo -e "\n---------------Installing webkit---------------6/8"
		apt-get -y install python-webkit-dev
		echo -e "\n---------------Installing openssl---------------7/8"
		apt-get -y install python-openssl

		echo -e "\nInstalling..."
		#echo "Installing packages needed if not already installed"
		#apt-get install -y git expect
		cd /opt/
		echo "Downloading the latest code..."
		git clone https://code.google.com/p/rachel/
		cd rachel
		./installclient.sh

		echo -e "\nInstallation Completed Successfully!"

		echo -e "\nThe system needs to reboot for the changes to take effect."
		echo "Reboot now? [yes/no]: "
		read reboot

		if [ "$reboot" == "yes" ]
		then
			echo -e "\nRebooting..."
			sudo /sbin/reboot
		else
			exit 0
		fi
	


	elif [ "$response" == "uninstall" ]
	then
		echo "Killing all rachel processes..."
		skill -STOP -u rachel
		userdel -r rachel

		echo -e "\nRemoving program..."
		rm -rf /opt/rachel/

		echo -e "\nUninstallation Completed Successfully!"


	elif [ "$response" == "update" ]
	then
		echo "Removing old program..."
		rm -rf /opt/rachel/
		cd /opt/
		echo "Downloading new program..."
		git clone https://code.google.com/p/rachel/
		cd rachel
		./installclient.sh
		echo -e "\nUpdate Completed Successfully!"
	fi
else
	echo "Sorry $response is not an available option.  Exiting."
	exit 0
fi

